package com.xmh.ffmpeg;

import com.gargoylesoftware.htmlunit.ScriptException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
import com.gargoylesoftware.htmlunit.javascript.host.Window;

/**
 * 自定义JavaScript解析器（目的是为了不打印js存在的错误到日志）
 * 
 * @author 小风
 * @datetime 2016年6月23日 下午10:19:40
 */
public class MyJavaScriptEngine extends JavaScriptEngine {

	public MyJavaScriptEngine(WebClient webClient) {
		super(webClient);
	}

	@Override
	protected void handleJavaScriptException(final ScriptException scriptException, final boolean triggerOnError) {
		// Trigger window.onerror, if it has been set.
		HtmlPage page = scriptException.getPage();
		if (triggerOnError && page != null) {
			final WebWindow window = page.getEnclosingWindow();
			if (window != null) {
				final Window w = (Window) window.getScriptableObject();
				if (w != null) {
					try {
						w.triggerOnError(scriptException);
					} catch (final Exception e) {
						handleJavaScriptException(new ScriptException(page, e, null), false);
					}
				}
			}
		}
/*		final JavaScriptErrorListener javaScriptErrorListener = getWebClient().getJavaScriptErrorListener();
		if (javaScriptErrorListener != null) {
			javaScriptErrorListener.scriptException(page, scriptException);
		}*/
		// Throw a Java exception if the user wants us to.
/*		if (getWebClient().getOptions().isThrowExceptionOnScriptError()) {
			throw scriptException;
		}*/
		// Log the error; ScriptException instances provide good debug info.
		// LOG.info("Caught script exception", scriptException);
	}

}